package org.msfj.admin.setting.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.msfj.admin.model.Log;
import org.msfj.admin.model.User;
import org.msfj.admin.org.service.UserService;
import org.msfj.admin.vo.LogVo;
import org.msfj.admin.vo.UserVo;
import org.msfj.admin.model.Log;
import org.msfj.admin.model.User;
import org.msfj.admin.org.service.UserService;
import org.msfj.admin.vo.LogVo;
import org.msfj.admin.vo.UserVo;
import org.msfj.admin.model.Log;
import org.msfj.admin.model.User;
import org.msfj.admin.org.service.UserService;
import org.msfj.admin.setting.service.LogService;
import org.msfj.admin.vo.LogVo;
import org.msfj.admin.vo.UserVo;
import org.msfj.common.annotation.Constants;
import org.msfj.common.base.BaseServiceImpl;
import org.msfj.common.enums.Enable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 系统日志service实现类
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2017-08-10 17:30:20
 * @Version: 1.0
 * @Created in idea by autoCode
 */

@Service
@CacheConfig(cacheNames = Constants.CACHE_NAMESPACE + "log")
public class LogServiceImpl extends BaseServiceImpl<Log> implements LogService {

    @Autowired
    private UserService userService;

    /**
     * 根据条件分页查询系统日志列表
     *
     * @param page 当前第几页
     * @param rows 每页多少条记录
     * @return PageInfo<Log>
     */
    @Override
    //@Cacheable
    public PageInfo<LogVo> getByLog(String notes, List<Long> userIds, List<String> selectDateTime, int page, int rows) {
        Example example = new Example(Log.class);
        Example.Criteria criteria = example.createCriteria();
        if (ObjectUtil.isNotNull(userIds)) {
            criteria.andIn("userId",userIds);
        }
        if (StrUtil.isNotBlank(notes)) {
            criteria.andLike("notes","%".concat(notes).concat("%"));
        }
        if (ObjectUtil.isNotNull(selectDateTime)) {
            criteria.andBetween("updateTime", DateUtil.parse(selectDateTime.get(0)), DateUtil.parse(selectDateTime.get(1)));
        }
        PageHelper.startPage(page, rows);
        List<Log> logList = super.getByExample(example);
        PageInfo logVoPageInfo = new PageInfo(logList);
        logVoPageInfo.setList(setOtherProperties(logList));
        return logVoPageInfo;
    }

    /**
     * 根据条件查询所有系统日志对象列表
     *
     * @param logVo
     * @return List<Log>
     */
    @Override
    //@Cacheable
    public List<LogVo> getAllLog(LogVo logVo){
        Example example = new Example(Log.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        List<LogVo> logVoList = setOtherProperties(super.getByExample(example));
        return logVoList;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param log
     * @return DepartmentVo
     */
    @Override
    //@Cacheable
    public LogVo setOtherProperties(Log log) {
        LogVo logVo = new LogVo();
        BeanUtils.copyProperties(log, logVo);
        if (logVo.getUserId()!=null) {
            User user = userService.getById(logVo.getUserId());
            logVo.setUserName(user.getName());
        } else {
            logVo.setUserName("");
        }
        logVo.setTimeBetween(DateUtil.formatBetween(logVo.getTime()));
        return logVo;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param logs
     * @return List<LogVo>
     */
    @Override
    //@Cacheable
    public List<LogVo> setOtherProperties(List<Log> logs) {
        List<LogVo> logVos = new ArrayList<>();
        List<UserVo> userVos = userService.getAllUser(null);
        if (CollectionUtil.isNotEmpty(logs)) {
            for (Log log : logs) {
                LogVo logVo = new LogVo();
                BeanUtils.copyProperties(log, logVo);
                if (CollectionUtil.isNotEmpty(userVos)) {
                    for (UserVo userVo : userVos) {
                        if (logVo.getUserId().equals(userVo.getId())) {
                            logVo.setUserName(userVo.getName());
                        }
                    }
                }
                logVo.setTimeBetween(DateUtil.formatBetween(logVo.getTime()));
                logVos.add(logVo);
            }
        }
        return logVos;
    }
}
